473,461 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamically create an anchor tag

Well,

I've a little trouble that seems unsolvable. It's a stupid question
that born because IE doesn't respect the standard as its usual.

But go to the problem. I create an anchor tag element, set some
properties, but not all works as it should. This is my code.

var anchorTag = document.createElement('a');
anchorTag.appendChild(document.createTextNode(x));

var pathImg = x.replace(' ', '_').replace('č', 'e');
pathImg = "images/" + pathImg + ".jpg";

anchorTag.href = "#";
anchorTag.setAttribute('onclick', 'popImage("' + pathImg + '", "' + x +
'")');
colorSpan.appendChild(anchorTag);
Doing in this way works fine just for Firefox and other browser. IE,
unfortunately, ignore the setAttribute method name.
The only way I've found to let it work on IE and Firefox too is the
line below.

//anchorTag.onclick = function() { "popImage('" + pathImg + "', '" + x
+ "')" }

Unfortunately, since the code above is nested into a for ... next loop
the code above not assign the pathImg value and x value to the content
of the in-line function but just a reference, and this cause an obvious
problem. All elements onclick event call through the popImage function
the same image.
Is there a way to solve this. Where am I doing mistake?

I've tried using the eval() method without results.

Thanks for any help.
Andrew

Dec 12 '06 #1
1 17623

Andrew wrote:
Well,

I've a little trouble that seems unsolvable. It's a stupid question
that born because IE doesn't respect the standard as its usual.
The first part is due to an issue of implementation where there is
nothing to explicitly say how things should work. The second part is
about closures.

[...]
The only way I've found to let it work on IE and Firefox too is the
line below.

//anchorTag.onclick = function() { "popImage('" + pathImg + "', '" + x
+ "')" }

Unfortunately, since the code above is nested into a for ... next loop
the code above not assign the pathImg value and x value to the content
of the in-line function but just a reference, and this cause an obvious
problem. All elements onclick event call through the popImage function
the same image.
Yes, because you have a closure back to pathImg and x, all of the
onclick functions reference back to the same value, which is whatever
they were set to last.
Is there a way to solve this. Where am I doing mistake?
There is a thread here that will help with both questions:

Subject: function syntax question
<URL;
http://groups.google.com.au/group/co...0caf593c683e27
>
--
Rob

Dec 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Mark Johnson | last post by:
You have an, a, anchor with href link. Can you use a stylesheet to effectively disable the link, so that you can't click on it; that it will appear simply as text?
2
by: Astra | last post by:
Hi everybody Need your help. I have a DB-extracted list of say 5 items per page, which have links on each one that takes you to more detailed info on the 'clicked' particular item. When you...
27
by: Nicholas Couch | last post by:
I have a little form with a couple of dynamically generated list boxes. When the user makes a selection from the first box, the second box is refreshed. When they make a selection from the second...
5
by: john_williams_800 | last post by:
Hi; I am just starting to use the DOM to do some more advanced javascripting so please be patient with my question if it is an ignorant question. I would like to use the DOM to dynamically...
0
by: Diane Yocom | last post by:
I'm very new to ASP.Net and probably jumped in a little over my head, but... I'm trying to create a user control that will control navigation through my site. It's sortof like Amazon.com, where...
11
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. ...
1
by: PraveenAnekalmat | last post by:
Hi everyone, I'm using anchor within the for loop which means the number of anchors(links) dynamically varies. Within the anchor i'm calling one javascript function and passing the dynamic value...
1
by: saikatbose2005 | last post by:
Hi, Ineed some help from anyone regarding an issue I'm facing. I've created an image dynamically on clicking a button. What I inetend to do is when I click on the created image it will popup a...
4
by: pbd22 | last post by:
Hi. I am trying to add an "onclick" 'event to an anchor tag and this is proving harder than I imagined. The anchor HAS NO ID. And, there will be tons of them on the page. So, I cannot access...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.